package com.sjgl.esdn.controller;

import com.sjgl.esdn.domain.AjaxResultESDN;
import com.sjgl.esdn.domain.entity.Column;
import com.sjgl.esdn.domain.entity.File;
import com.sjgl.esdn.domain.t.FileColumn;
import com.sjgl.esdn.service.ColumnService;
import com.sjgl.esdn.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 专栏控制层
 *
 * @author 缪恒铭
 * @date 2022/08/11
 */
@RestController
@RequestMapping("/column")
public class ColumnController extends BaseControllerESDN {
    @Autowired
    ColumnService columnService;

    @Autowired
    FileService fileService;

    /**
     * 创建专栏
     *
     * @param column 专栏
     * @return {@link AjaxResultESDN}
     */
    @PostMapping("create")
    public AjaxResultESDN createColumn(@RequestBody Column column) {//Long userId,@RequestBody String columnName, String intro
        if (columnService.insertColumn(column.getUserId(), column.getColumnName(), column.getColumnIntro()) > 0) {
            return AjaxResultESDN.success("创建成功");
        }
        return AjaxResultESDN.error("专栏昵称冲突");
    }

    /**
     * 修改专栏
     *
     * @param column 专栏
     * @return {@link AjaxResultESDN}
     */
    @PostMapping("rename")
    public AjaxResultESDN renameColumn(@RequestBody Column column) {
        if (columnService.updateColumn(column.getColumnId(), column.getColumnName(), column.getColumnIntro(), column.getUserId()) > 0) {
            return AjaxResultESDN.success("命名成功");
        }
        return AjaxResultESDN.error("专栏昵称冲突");
    }

    /**
     * 删除专栏
     *
     * @param columnId 专栏id
     * @return {@link AjaxResultESDN}
     */
    @DeleteMapping("delete")
    public AjaxResultESDN deleteColumn(Long columnId) {
        if (columnService.deleteColumn(columnId) > 0) {
            return AjaxResultESDN.success("删除成功");
        }
        return AjaxResultESDN.error("删除失败");
    }

    //参数待检查

    /**
     * 添加文件
     *
     * @param fileColumn 文件专栏
     * @return {@link AjaxResultESDN}
     */
    @PostMapping("addFile")
    public AjaxResultESDN addFile(@RequestBody FileColumn fileColumn) {
        Long columnId = fileColumn.getColumnId();
        Long fileId = fileColumn.getFileId();
        //检查参数
        if(columnService.selectColumnById(columnId)==null||!fileService.isFileExist(fileId)){
            return AjaxResultESDN.error("专栏或文件不存在");
        }

        //检查唯一性
        if (columnService.isColumnAndFileExist(columnId, fileId)) {
            return AjaxResultESDN.error("专栏中已存在该文件");
        }
        if (columnService.addFile(columnId, fileId) > 0) {
            return AjaxResultESDN.success("收藏成功");
        }
        return AjaxResultESDN.error("未知错误");
    }

    //分页

    /**
     * 获得文件列表
     *
     * @param columnId 专栏id
     * @return {@link AjaxResultESDN}
     * @throws IOException ioexception
     */
    @GetMapping("fileList")
    public AjaxResultESDN getFileList(Long columnId) throws IOException {
        AjaxResultESDN ajax = AjaxResultESDN.success();
        List<Long> fileIdList = columnService.selectFileIdListById(columnId);
        List<File> fileList = fileService.selectFileListByIdList(fileIdList);
        ajax.put(AjaxResultESDN.DATA_TAG, fileList);
        return ajax;
    }

    /**
     * 删除文件
     *
     * @param columnId 专栏id
     * @param fileId    文件id
     * @return {@link AjaxResultESDN}
     */
    @DeleteMapping("deleteFile")
    public AjaxResultESDN deleteFile(Long columnId, Long fileId) {
        //检查唯一性
        if (!columnService.isColumnAndFileExist(columnId, fileId)) {
            return AjaxResultESDN.error("专栏中不存在该文件");
        }
        if (columnService.deleteFile(columnId, fileId) > 0) {
            return AjaxResultESDN.success("已取消收藏");
        }
        return AjaxResultESDN.error("未知错误");
    }

    /**
     * 获得专栏列表
     *
     * @param userId 用户id
     * @return {@link AjaxResultESDN}
     */
    @GetMapping("list")
    public AjaxResultESDN getColumnList(Long userId) {
        AjaxResultESDN ajax = AjaxResultESDN.success();
        List<Long> columnIdList = columnService.selectColumnIdListByUserId(userId);
        List<Column> columnList = columnService.selectColumnListByColumnIdList(columnIdList);
        ajax.put(AjaxResultESDN.DATA_TAG, columnList);
        return ajax;
    }

    /**
     * 获得专栏列表
     *
     * @return {@link AjaxResultESDN}
     */
    @GetMapping("getColumnList")
    public AjaxResultESDN getColumnList() {
        AjaxResultESDN ajax = AjaxResultESDN.success();
        List<Column> columnList = columnService.selectColumnList();
        ajax.put(AjaxResultESDN.DATA_TAG, columnList);
        return ajax;
    }
}
